/**
 * @author yxw
 * date : 2013年10月12日 下午3:13:20 
 */
package com.tzinfo.tztbase;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.galhttprequest.GalHttpRequest;
import com.galhttprequest.GalHttpRequest.GalHttpLoadTextCallBack;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tzinfo.data.TztData;
import com.tzinfo.data.UserInfo;
import com.tzinfo.tzt.R;
import com.tzinfo.tztbase.util.DataSaveTools;
import com.yxw.util.OutLog;
import com.yxw.view.YXWProgressDialog;
import com.yxw.view.util.ResFinder;

public class TztBaseRegistActivity extends Activity {
	private Dialog dialog;
	private UserInfo userInfo;
	private EditText uNameEditText, pwdEditText;
	private CheckBox isCheck;
	private String username = null;
	private String password = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tztbase_ac_regist);
		init();
	}

	private void init() {

		uNameEditText = (EditText) ResFinder.findViewById(this, "R.id.et_username");
		pwdEditText = (EditText) ResFinder.findViewById(this, "R.id.et_password");

		isCheck = (CheckBox) ResFinder.findViewById(this, "R.id.check_accept_agreement");

		dialog = new YXWProgressDialog(this).createDialog("加载中…");
	}

	// onclick
	public void bt_back(View v) {
		this.finish();
	}

	// onclick
	public void bt_regist(View v) {
		if (isCheck.isChecked()) {
			regist();
		} else {
			Toast.makeText(getApplicationContext(), "请先阅读用《户服务协议》", Toast.LENGTH_SHORT)
					.show();
		}
	}

	public void regist() {
		dialog.show();
		username = uNameEditText.getText().toString().trim();
		password = pwdEditText.getText().toString().trim();
		if (!TztData.checkLoginSubmitInfo(username, password)) {
			Toast.makeText(this, "请先输入帐号和密码！", Toast.LENGTH_SHORT).show();
			dialog.dismiss();
			return;
		}
		GalHttpRequest galHttpRequest = new GalHttpRequest(
				TztBaseRegistActivity.this);
		galHttpRequest.setCacheEnable(false);
		galHttpRequest.setURL("http://member.tzinfo.net/mob/api/Reg.ashx"
				+ "?username=" + username + "&"
				+ "password=" + password);
		galHttpRequest.startAsynRequestString(new GalHttpLoadTextCallBack() {

			@Override
			public void textLoaded(String arg0) {
				OutLog.LogE("JsonString", arg0 + "");
				Gson gson = new Gson();
				java.lang.reflect.Type type = new TypeToken<UserInfo>() {
				}.getType();
				userInfo = gson.fromJson(arg0, type);

				if (userInfo.getSuccess()) {
					Intent intent = new Intent();
					intent.putExtra(TztData.USER_ISLOGIN, true);
					TztData.isUserLogin = true;
					TztData.userInfo = userInfo;
					DataSaveTools.saveLoginInfo(TztBaseRegistActivity.this, username, password);
					setResult(2, intent);
					dialog.dismiss();
					TztBaseMainActivity.requestUserPosition(TztBaseRegistActivity.this);
					finish();
				} else {
					DataSaveTools.clearLoginInfo(TztBaseRegistActivity.this);
					getDataError(userInfo.getMsg());
				}
			}
		});
	}


	private void getDataError(String ERRORMESSAGE) {
		new AlertDialog.Builder(this).setCancelable(false)
				.setMessage(ERRORMESSAGE)
				.setPositiveButton("取消" ,new DialogInterface.OnClickListener()

				{
					public void onClick

					(DialogInterface dialog, int which) {
						finish();
					}
				}).setNegativeButton("重试", new DialogInterface.OnClickListener()

				{
					public void onClick(

					DialogInterface dialogInterface, int

					whichButton) {
						regist();
					}
				}).show();
	}

}
